package GUILayer;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import CtrLayer.*;
import ModelLayer.*;


/**
 * 
 * This class creates a mainGUIPanel that belongs to one employee.
 * 31.05.2012
 * @author Lindgren
 * @Version 0.1
 */
public class MainGUI { 


	private Employee employee;
	private JPanel mainGUIPanel;

	/**
	 * Create the mainGUIPanel.
	 * @param employee The employee that 'owns' this panel.
	 */
	public MainGUI(Employee employee) { 
		this.employee = employee;
		initialize();
		System.out.println("MainGUI initialized!");
	}

	/**
	 * Initializes a mainGUIpanel.
	 */
	public void initialize()
	{
		System.out.println("MainGUI initializing!");
		mainGUIPanel = new JPanel();
		mainGUIPanel.setToolTipText("mainGUIPanel");
		mainGUIPanel.setName(employee.getLogin());
		MainFrame.getMainGUIPanels().put(employee.getLogin(), mainGUIPanel);
		System.out.println("MainGUI mainGUIPanel mapped: "+MainFrame.getMainGUIPanels().get(employee.getLogin()));
		mainGUIPanel.setLayout(new GridLayout(0, 2, 0, 0));

		JPanel panel = new JPanel();
		panel.setBackground(Color.WHITE);
		mainGUIPanel.add(panel);
		panel.setLayout(new BorderLayout(0, 0));

		JLabel lblMainguipanel = new JLabel("");
		lblMainguipanel.setToolTipText("mainGUIPanel");
		lblMainguipanel.setHorizontalAlignment(SwingConstants.CENTER);
		lblMainguipanel.setIcon(new ImageIcon(MainGUI.class.getResource("/GUILayer/Images/logo.jpg")));
		panel.add(lblMainguipanel);

		JPanel menuPanel = new JPanel();
		menuPanel.setToolTipText("menuPanel");
		mainGUIPanel.add(menuPanel);

		JButton btnSalesMenu = new JButton("Sales Menu");
		btnSalesMenu.setForeground(Color.WHITE);
		btnSalesMenu.setBackground(Color.GREEN);
		btnSalesMenu.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				openSaleGUI();
			}
		});

		JButton btnEvents = new JButton("Events");
		btnEvents.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				openEventGUI();
			}
		});

		JButton btnItems = new JButton("Items");
		btnItems.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				//TODO openItemGUI();
			}
		});

		JButton btnSchedule = new JButton("Schedule");
		btnSchedule.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//TODO openScheduleGUI();
			}
		});

		JButton btnStatistics = new JButton("Statistics");
		btnStatistics.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				//TODO openStatisticsGUI();
			}
		});

		JButton btnLogOut = new JButton("Log Out");
		btnLogOut.setForeground(Color.WHITE);
		btnLogOut.setBackground(Color.RED);
		btnLogOut.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				logOut();
			}
		});

		menuPanel.setLayout(new GridLayout(0, 1, 3, 3));
		menuPanel.add(btnSalesMenu);
		menuPanel.add(btnEvents);
		menuPanel.add(btnItems);
		menuPanel.add(btnSchedule);
		menuPanel.add(btnStatistics);
		menuPanel.add(btnLogOut);
	}


	/**
	 *  Opens the saleGUI
	 */
	private void openSaleGUI()
	{
		try {
			System.out.println("Sales button pressed");		
			System.err.println("MainGUI employee: "+employee.getEmpId());	
			SaleCtr saleCtr = SaleCtr.getInstance();
			Sale sale = saleCtr.createSale(employee);
			MainFrame.getUserFramePanels().get(employee.getLogin()).remove(MainFrame.getMainGUIPanels().get(employee.getLogin()));
			MainFrame.getUserFramePanels().get(employee.getLogin()).add(MainFrame.getSaleGUIPanels().get(employee.getLogin()));
			MainFrame.getSaleGUIPanels().get(employee.getLogin()).repaint();
			SaleGUI saleGUI = MainFrame.getSaleGUIs().get(employee.getLogin());
			saleGUI.setSale(sale);
			saleGUI.updateSalesList();
		} catch (Exception e) {
			System.err.println("openSaleGUI "+employee.getLogin()+" Failed");
			e.printStackTrace();
		}
	}

	/**
	 * Opens the eventGUI
	 */
	private void openEventGUI()
	{
		try {
			MainFrame.getUserFramePanels().get(employee.getLogin()).remove(MainFrame.getMainGUIPanels().get(employee.getLogin()));
			MainFrame.getUserFramePanels().get(employee.getLogin()).add(MainFrame.getEventGUIPanels().get(employee.getLogin()));
			MainFrame.getEventGUIs().get(employee.getLogin()).getTabbedPaneEvents().setSelectedIndex(0);
			MainFrame.getEventGUIPanels().get(employee.getLogin()).repaint();
			MainFrame.getEventGUIs().get(employee.getLogin()).updateEvents();
		} catch (Exception e) {
			System.err.println("openEventGUI "+employee.getLogin()+" Failed");
			e.printStackTrace();
		}
	}


	/**
	 * Logs the user out.
	 */
	private void logOut()
	{
		try {
			int tabIndex = LoginGUI.getTabIndex();
			tabIndex--;
			LoginGUI.setTabIndex(tabIndex);
			MainFrame.getTabbedPane().remove(MainFrame.getUserFramePanels().get(employee.getLogin()));
			System.out.println("User "+employee.getLogin()+" has logged out");
			LoginGUI.getEmployees().remove(employee.getLogin());
		} catch (Exception e) {
			System.err.println("LogOut "+employee.getLogin()+" Failed");
			e.printStackTrace();
		}
	}
}
